package com.bjpowernode.money.web;

import com.alibaba.dubbo.config.annotation.Reference;
import com.bjpowernode.money.PageModel;
import com.bjpowernode.money.model.BidInfo;
import com.bjpowernode.money.model.LoanInfo;
import com.bjpowernode.money.service.BidInfoService;
import com.bjpowernode.money.service.LoanInfoService;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class LoanInfoController {

    @Reference(interfaceClass = LoanInfoService.class,version = "1.0.0",timeout = 20000,check = false)
    LoanInfoService loanInfoService;

    @Reference(interfaceClass = BidInfoService.class,version = "1.0.0",timeout = 20000,check = false)
    BidInfoService bidInfoService;

    /**
     * 查看所有的散标类型的产品
     *    访问需要传入参数 类型决定是新手宝还是散标
     *
     * @param ptype
     * @return
     */
    @RequestMapping("/loan/loan")
    public String loan(@RequestParam(name = "ptype",required = true) Integer ptype,
                       Long cunPage, Model model, HttpServletRequest request){

        //1、产品列表信息：类型，分页信息
        /***
         * 分页信息:共20条3页　当前为第2页　 首页 上一页 下一页 尾页
         * 不变：总记录数  总页数（尾页）  当前页  首页  每页显示多少条  ==》Map ==》分页模型
         * 变：上一页  下一页
         * 分析 抽象 封装 继承 多态
         * 1  0-9   (1-1)*10  0
         * 2 10-19  (2-1)*10  10
         * 3 20-29
         *
         *   start = 当前页-1 乘以 每页的记录数
         *   content = 每页的记录数
         */

        PageModel pageModel = (PageModel) request.getSession().getAttribute("pageModel");
        if(pageModel==null){
            //说明是第一次登录 还没有将分页模型放入到session中
            pageModel = new PageModel(9);
            request.getSession().setAttribute("pageModel",pageModel);
        }

        //查看总记录数
        Long totalCount =loanInfoService.queryLoanInfoCountByType(ptype);
        pageModel.setTotalCount(totalCount);

        //服务端验证，防止在地址栏中输入不正常的页数

        //如果没有当前页，默认访问首页
        if(cunPage==null || cunPage<pageModel.getFirstPage() ){
            cunPage=pageModel.getFirstPage().longValue();
        }

        //当输入的页数大于总页数
        if(cunPage>pageModel.getTotalPage()){
            //大于总页数 让当前页就为尾页
            cunPage = pageModel.getTotalPage();
        }

        pageModel.setCunPage(cunPage);

        List<LoanInfo> loanInfos = loanInfoService.queryLoanInfosByTypeAndPageModel(ptype,pageModel);
        model.addAttribute("loanInfos",loanInfos);
        model.addAttribute("ptype",ptype);

        return "loan";
    }

    //跳转到产品详情页面
    @RequestMapping("/loan/loanInfo")
    //@ResponseBody  加这个竟然跳转不了页面，直接得到了字符串
    public String loanInfo(@RequestParam(name = "loanId",required = true) int loanId,Model model,
                           Long cunPage,HttpServletRequest request){

        //根据id查询对应的产品信息
        LoanInfo loanInfo = loanInfoService.queryLoanInfoById(loanId);
        model.addAttribute("loanInfo",loanInfo);

        //实现投资记录信息的 分页

        //说明是第一次登录 还没有将分页模型放入到session中
        PageModel pageModel2 = (PageModel) request.getSession().getAttribute("pageModel2");
        if(pageModel2==null){
            pageModel2 = new PageModel(10);
            request.getSession().setAttribute("pageModel2",pageModel2);
        }

        //查看总记录数
        Long totalCount =bidInfoService.queryBidInfoCountByLoadId(loanId);
        pageModel2.setTotalCount(totalCount);

        //如果没有当前页，默认访问首页
        if(cunPage==null || cunPage<pageModel2.getFirstPage() ){
            cunPage=pageModel2.getFirstPage().longValue();
        }

        pageModel2.setCunPage(cunPage);

        //根据loadId查询对应的BidInfo 投资信息
        List<BidInfo> bidInfos = bidInfoService.queryBidInfoByLoadId(loanId,pageModel2);
        model.addAttribute("bidInfos",bidInfos);
       // System.out.println(model.getAttribute("bidInfos"));

        return "loanInfo";
    }



}
